﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Core.Data;
using Forest.Cat.Domain.Entities;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Core;

namespace Forest.Cat.Domain.Services.JobHunting
{
    public partial class SavedCompanyService : ISavedCompanyService
    {
        #region Constants
        private const string SAVEDCOMPANY_BY_ID_KEY = "savedcompany.id-{0}";
        private const string SAVEDCOMPANY_PATTERN_KEY = "savedcompany.";
        #endregion

        #region Fields
        private readonly IRepository<SavedCompany> _savedCompanyRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public SavedCompanyService(IRepository<SavedCompany> savedCompanyRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _savedCompanyRepository = savedCompanyRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteSavedCompany(SavedCompany savedcompany)
        {
            if (savedcompany == null)
                throw new ArgumentNullException("SavedCompany");

            _savedCompanyRepository.Delete(savedcompany);
            _cacheManager.RemoveByPattern(SAVEDCOMPANY_PATTERN_KEY);
            _eventPublisher.EntityDeleted(savedcompany);
        }

        public SavedCompany GetSavedCompanyById(Guid savedcompanyId)
        {
            if (savedcompanyId == Guid.Empty)
                return null;
            string key = string.Format(SAVEDCOMPANY_BY_ID_KEY, savedcompanyId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _savedCompanyRepository.GetById(savedcompanyId);
                return cl;
            });
        }

        public virtual IPagedList<SavedCompany> GetAllSavedCompanys(int pageIndex, int pageSize)
        {
            var query = _savedCompanyRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<SavedCompany>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertSavedCompany(SavedCompany savedcompany)
        {
            if (savedcompany == null)
                throw new ArgumentNullException("SavedCompany");
            _savedCompanyRepository.Insert(savedcompany);
            _cacheManager.RemoveByPattern(SAVEDCOMPANY_PATTERN_KEY);
            _eventPublisher.EntityInserted(savedcompany);
        }

        public virtual void UpdateSavedCompany(SavedCompany savedcompany)
        {
            if (savedcompany == null)
                throw new ArgumentNullException("SavedCompany");
            _savedCompanyRepository.Update(savedcompany);
            _cacheManager.RemoveByPattern(SAVEDCOMPANY_PATTERN_KEY);
            _eventPublisher.EntityUpdated(savedcompany);
        }
        #endregion

        #region Extend Methods
        /// <summary>
        /// Search PageList by JobseekerId
        /// </summary>
        /// <param name="jobseekerId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<SavedCompany> GetSavedCompanysByJobseekerId(Guid jobseekerId, int pageIndex, int pageSize)
        {
            var query = _savedCompanyRepository.Table.Where(item => item.JobseekerId == jobseekerId).OrderByDescending(o =>
                o.DateSaved);
            var items = new PagedList<SavedCompany>(query, pageIndex, pageSize);
            return items;
        }

        public IQueryable<SavedCompany> GetSavedCompanysByJobseekerId(Guid jobseekerId)
        {
            return _savedCompanyRepository.Table.Where(item => item.JobseekerId == jobseekerId);
        }
        #endregion
    }
}
